package cn.com.blueInfo.bpm.system.service.impl;

import cn.com.blueInfo.bpm.system.convert.ApplicationConvert;
import cn.com.blueInfo.bpm.system.entity.Application;
import cn.com.blueInfo.bpm.system.repository.ApplicationRepository;
import cn.com.blueInfo.bpm.system.request.ApplicationDTO;
import cn.com.blueInfo.bpm.system.response.ApplicationVO;
import cn.com.blueInfo.bpm.system.service.ApplicationService;
import cn.com.blueInfo.core.config.CommonConfig;
import cn.com.blueInfo.core.util.SnowflakeIdGenerator;
import cn.com.blueInfo.utils.result.ResultInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 租户系统表 业务服务实现类
 * @author suxch
 * @since 2025-09-01 19:17:41
 */
@Log4j2
@Service
public class ApplicationServiceImpl implements ApplicationService {

    @Autowired
    private ApplicationRepository applicationRepository;

    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;

    @Autowired
    private ApplicationConvert applicationConvert;

    @Autowired
    private CommonConfig commonConfig;

    /**
     * 新增租户系统
     * @param applicationDTO 租户系统参数
     */
    @Override
    public ResultInfo<Integer> addApplication(ApplicationDTO applicationDTO) {
        if (commonConfig.isSupper()) {
            applicationDTO.setTenantId(null);
            applicationDTO.setCreateUserId(null);
        }
        Application application = applicationConvert.dtoToEntity(applicationDTO);
        application.setId(snowflakeIdGenerator.nextId());
        Integer addSize = applicationRepository.createApplication(application);
        return ResultInfo.<Integer>builder().data(addSize).message("租户系统成功新增 {} 条").build();
    }

    /**
     * 批量新增租户系统
     * @param applicationDTOList 租户系统参数列表
     */
    @Override
    public ResultInfo<Integer> batchAddApplication(List<ApplicationDTO> applicationDTOList) {
        List<Application> applicationList = applicationConvert.dtoToEntity(applicationDTOList);
        for (Application application : applicationList) application.setId(snowflakeIdGenerator.nextId());
        Integer addSize = applicationRepository.batchCreateApplication(applicationList);
        return ResultInfo.<Integer>builder().data(addSize).message("租户系统成功新增 {} 条").build();
    }

    /**
     * 删除租户系统
     * @param applicationId 租户系统ID
     */
    @Override
    public ResultInfo<Integer> removeApplication(Long applicationId) {
        Integer removeSize = applicationRepository.deleteApplication(applicationId);
        return ResultInfo.<Integer>builder().data(removeSize).message("租户系统成功删除 {} 条").build();
    }

    /**
     * 批量删除租户系统
     * @param applicationIdList 租户系统ID列表
     */
    @Override
    public ResultInfo<Integer> batchRemoveApplication(List<Long> applicationIdList) {
        Integer removeSize = applicationRepository.batchDeleteApplication(applicationIdList);
        return ResultInfo.<Integer>builder().data(removeSize).message("租户系统成功删除 {} 条").build();
    }

    /**
     * 更新部分租户系统
     * @param applicationUpdatePartialDTO 租户系统参数
     */
    @Override
    public ResultInfo<Integer> modifyApplicationPartial(
            ApplicationDTO.UpdatePartialDTO applicationUpdatePartialDTO) {
        if (commonConfig.isSupper()) {
            applicationUpdatePartialDTO.setCreateUserId(null);
        }
        Application application = applicationConvert.dtoToEntity(applicationUpdatePartialDTO);
        Integer modifySize = applicationRepository.updateApplicationPartial(application);
        return ResultInfo.<Integer>builder().data(modifySize).message("租户系统成功更新 {} 条").build();
    }

    /**
     * 更新全部租户系统
     * @param applicationDTO 租户系统参数
     */
    @Override
    public ResultInfo<Integer> modifyApplication(ApplicationDTO applicationDTO) {
        if (commonConfig.isSupper()) {
            applicationDTO.setTenantId(null);
            applicationDTO.setCreateUserId(null);
        }
        Application application = applicationConvert.dtoToEntity(applicationDTO);
        Integer modifySize = applicationRepository.updateApplication(application);
        return ResultInfo.<Integer>builder().data(modifySize).message("租户系统成功更新 {} 条").build();
    }

    /**
     * 查询一条租户系统
     * @param applicationId 租户系统ID
     */
    @Override
    public ResultInfo<ApplicationVO> queryApplicationById(Long applicationId) {
        Application application = applicationRepository.getApplicationById(applicationId);
        ApplicationVO applicationVO = applicationConvert.entityToVO(application);
        return ResultInfo.<ApplicationVO>builder().data(applicationVO).message("租户系统查询成功").build();
    }

    /**
     * 分页查询租户系统
     * @param applicationQueryDTO 租户系统参数
     */
    @Override
    public ResultInfo<Page<ApplicationVO>> queryApplicationPage(ApplicationDTO.QueryDTO applicationQueryDTO) {
        Page<Application> applicationPage = applicationRepository.getApplicationPage(applicationQueryDTO);
        Page<ApplicationVO> resultData = applicationConvert.convertPage(applicationPage);
        return ResultInfo.<Page<ApplicationVO>>builder()
                .data(resultData)
                .message("租户系统分页查询成功，共 {} 条", resultData.getRecords().size())
                .build();
    }

    /**
     * 根据条件查询 租户系统 数量
     * @param applicationQueryDTO 租户系统参数
     */
    @Override
    public ResultInfo<Long> queryApplicationCount(ApplicationDTO.QueryDTO applicationQueryDTO) {
        Long resultCount = applicationRepository.getApplicationCount(applicationQueryDTO);
        return ResultInfo.<Long>builder()
                .data(resultCount)
                .message("租户系统数量查询成功，共 {} 条", resultCount)
                .build();
    }

    /**
     * 根据条件查询 租户系统
     * @param applicationQueryDTO 租户系统参数
     */
    @Override
    public ResultInfo<List<ApplicationVO>> queryApplicationList(ApplicationDTO.QueryDTO applicationQueryDTO) {
        List<Application> applicationList = applicationRepository.getApplicationList(applicationQueryDTO);
        List<ApplicationVO> resultData = applicationConvert.entityListToVoList(applicationList);
        return ResultInfo.<List<ApplicationVO>>builder()
                .data(resultData)
                .message("租户系统分页查询成功，共 {} 条", resultData.size())
                .build();
    }
}
